package com.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.ddd.wandemo.R;

/**
 * 显示多种状态的layout，断网，没数据，loading，正常
 */
public class PageLayoutTest extends FrameLayout {
    public static final int NORMAL = 10000;
    public static final int NO_NET = 10001;
    public static final int NO_DATA = 10003;
    public static final int LOADING = 10004;
    public static final int ERROR = 10005;

    private int curState = 0;
    private Context mContext;
    private LayoutInflater mInflater;

    private View noDataPageView;
    private View noNetPageView;
    private View normalPageView;
    private View loadingPageView;
    private View errorPageView;
    private LayoutParams mParams;
    private TextView mTvReload;

    private PageLayoutClickListener mClickListener;
    private TextView mTvError;

    public PageLayoutTest(@NonNull Context context) {
        this(context, null);
    }

    public PageLayoutTest(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PageLayoutTest(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mInflater = LayoutInflater.from(context);
        //            mParams = new LayoutParams(DensityUtils.dp2px(mContext,100f),DensityUtils.dp2px(mContext,100f));
        mParams = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mParams.gravity = Gravity.CENTER;
    }


    public PageLayoutTest setNoDataPage(int layoutId) {
        noDataPageView = mInflater.inflate(layoutId, this, false);
        noDataPageView.setVisibility(View.GONE);
        addView(noDataPageView, mParams);
        return this;
    }

    public PageLayoutTest setNoNetPage(int layoutId) {
        noNetPageView = mInflater.inflate(layoutId, this, false);
        noNetPageView.setVisibility(View.GONE);
        addView(noNetPageView, mParams);
        return this;
    }


    public PageLayoutTest setNormalPage(int layoutId) {
        normalPageView = mInflater.inflate(layoutId, this, false);
        normalPageView.setVisibility(View.GONE);
        addView(normalPageView, mParams);
        return this;
    }


    public PageLayoutTest setLoading(int layoutId) {
        loadingPageView = mInflater.inflate(layoutId, this, false);
        loadingPageView.setVisibility(View.GONE);
        addView(loadingPageView, mParams);
        return this;
    }

    public PageLayoutTest setError(int layoutId) {
        errorPageView = mInflater.inflate(layoutId, this, false);
        errorPageView.setVisibility(View.GONE);
        addView(errorPageView, mParams);
        return this;
    }


    public PageLayoutTest build() {
        initView();
        initEvent();
        return this;
    }


    private void initView() {
        mTvReload = noNetPageView.findViewById(R.id.tv_reload);
        mTvError = errorPageView.findViewById(R.id.tv_page_error);
    }


    public void setErrorMsg(String msg){
        mTvError.setText(msg);
    }

    public void setNoNetMsg(String msg){
        mTvReload.setText(msg);
    }
    private void initEvent() {
        mTvReload.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onReloadClick();
            }
        });
    }

    public void changePageState(int state) {
        if (curState == state) {
            return;
        }
        normalPageView.setVisibility(state == NORMAL ? View.VISIBLE : View.GONE);
        noNetPageView.setVisibility(state == NO_NET ? View.VISIBLE : View.GONE);
        noDataPageView.setVisibility(state == NO_DATA ? View.VISIBLE : View.GONE);
        loadingPageView.setVisibility(state == LOADING ? View.VISIBLE : View.GONE);
        errorPageView.setVisibility(state == ERROR ? View.VISIBLE : View.GONE);
        curState = state;
    }


    public void setNormalVisible() {
        normalPageView.setVisibility(normalPageView.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
    }

    public void setNoNetVisible() {
        noNetPageView.setVisibility(noNetPageView.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
    }

    public void setErrorVisible() {
        noDataPageView.setVisibility(noDataPageView.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
    }

    public void setLoadingVisible() {
        loadingPageView.setVisibility(loadingPageView.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
    }


    public interface PageLayoutClickListener{
        void onReloadClick();
    }

    public void setPageLayoutClickListener(PageLayoutClickListener listener){
        this.mClickListener = listener;
    }
}
